<?php
/**
 * @file $HeadURL: brand.php $
 * @author $Author: LDD
 * @date $Date: 2014-12-26
 * @brief 
 */
namespace haibao\www\business;

use \haibao\www\model\filter\Brand as BrandFilter;
use \haibao\frame\data\query\Condition ;
use \haibao\www\data\Brand as BrandData;
use \haibao\www\common\ChineseSpell ;
use \haibao\www\business\Tag as TagBusiness;
use \haibao\www\model\data\Tag as TagModel;
use \haibao\www\data\Tag as TagData;
use \haibao\www\model\data\Brand as BrandModel;
use haibao\www\web\view\brand\AddBrand;

class Brand extends \haibao\cms\business\Base{
	public static $brandRegion = array(
			BrandModel::REGION_WESTERN =>'欧美',
			BrandModel::REGION_JAPAN =>'日本',
			BrandModel::REGION_KOREA =>'韩国',
			BrandModel::REGION_HONGKONG_OR_SOUTHEAST=>'港台/东南亚',
			BrandModel::REGION_MAINLAND =>'内地',
			BrandModel::REGION_OTHER => '其他'
	);
	public static $brandProperty= array(
			BrandModel::BRAND_PROPERTY_LUXURY => '奢侈品品牌',
			BrandModel::BRAND_PROPERTY_HAUTE_COUTURE =>'高级定制品牌',
			BrandModel::BRAND_PROPERTY_DESIGNER =>'设计师品牌',
			BrandModel::BRAND_PROPERTY_FASHIONSHOP =>'时尚店品牌',
			BrandModel::BRAND_PROPERTY_HIGN_STREET =>'高街品牌',
			BrandModel::BRAND_PROPERTY_CHAOPAI =>'潮牌街牌',
			BrandModel::BRAND_PROPERTY_OUTDOOR =>'运动户外品牌',
			BrandModel::BRAND_PROPERTY_OTHERS =>'其他品牌'
	);
	//品牌类别
	public static $brandType = array(
			BrandModel::BRAND_TYPE_WOMENSWEAR =>"女装",
			BrandMOdel::BRAND_TYPE_MENSWEAR =>"男装",
			BrandModel::BRAND_TYPE_HANDBAG =>"手袋 / 鞋靴 / 箱包",
			BrandModel::BRAND_TYPE_SCARF =>"丝巾 / 帽袜 / 配饰",
			BrandModel:: BRAND_TYPE_WEDDING_DRESS =>"婚纱礼服",
			BrandModel::BRAND_TYPE_CHILDRENWEAR =>"童装",
			BrandModel::BRAND_TYPE_UNDERWEAR =>"内衣",
			BrandModel::BRAND_TYPE_COWBOY =>"牛仔丹宁",
			BrandModel::BRAND_TYPE_OUTDOOR =>"运动/户外服饰",
			BrandModel::BRAND_TYPE_JEWELRY=>"珠宝 / 钟表",
			BrandModel::BRAND_TYPE_GLASSES =>"眼镜",
			BrandModel::BRAND_TYPE_BEAUTY_MAKEUP =>"美妆",
			BrandModel::BRAND_TYPE_MENSKIN =>"男士护肤 / 香水",
			BrandModel::BRAND_TYPE_XIHU =>"洗护",
			BrandModel::BRAND_TYPE_HOMEFURNISHING =>"家居饰品",
			BrandModel::BRAND_TYPE_FOOD_AND_WINE =>"美食与美酒",
			BrandModel::BRAND_TYPE_CAR =>"汽车",
			BrandModel::BRAND_TYEP_TREND =>"潮流数码",
			BrandModel::BRAND_TYPE_ELECTRICAL =>"生活电器",
			BrandModel::BRAND_TYPE_CARTOON =>"卡通玩偶 / 手绘插画"
	);
	/**
	 * 品牌中文名、品牌英文名判断是否已经存在
	 */
	public static function hasSameBrandName($nameCN,$nameEN,$brandId = null){
		$brandFilter = new BrandFilter();
		
		if(!$nameCN && !$nameEN){
			throw new BusinessException('名称不能为空',self::DATA_EMPTY_NAME);
		}
		if($nameCN){
			$brandFilter->where(BrandFilter::CONDITION_FIELD_NAME_NAMECN, Condition::CONDITION_EQUAL, $nameCN);
		}
		if($nameEN){
			$brandFilter->where(BrandFilter::CONDITION_FIELD_NAME_NAMEEN, Condition::CONDITION_EQUAL, $nameEN);
		}
		if($brandId){
			$brandFilter->where(BrandFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $brandId);
		}
		$brandFilter->where(BrandFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\Brand::STATUS_ENABLE);
		$brandData = new BrandData();
		$brandInfo = $brandData->getOne($brandFilter);
		if($brandInfo){
			return $brandInfo->Id;
		}else{
			return false;
		}
		
	}
	
	/**
	 * 添加新的品牌
	 * @param \haibao\cms\model\data\Brand $model
	 */
	public static function addBrand($model){
		self::checkOperation(self::FUNC_CODE_BRAND_ADD);
		
		if(!$model || !($model instanceof  \haibao\cms\model\data\Brand)){
			throw new BusinessException('品牌不能为空',self::DATA_EMPTY);
		}
		if(!$model->NameCN){
			throw  new BusinessException('品牌中文名不能为空',self::DATA_EMPTY_NAME);
		}else{
			$chineseSpell = new ChineseSpell();
			$chSpellFull  = $chineseSpell->getFullSpell($model->NameCN," ");
			$model->PinyinCN = strtolower(substr($chSpellFull,0,1));
		}
		if(!$model->ImgId){
			throw new BusinessException('图片不能为空',self::DATA_EMPTY_ID);
		}
		if(self::hasSameBrandName($model->NameCN, null)){
			throw new BusinessException('品牌中文名已存在',self::DATA_ERROR_NAME_EXIST);
		}
		if(!$model->NameEN){
			$model->NameEN = $chSpellFull;
		}
		$model->PinyinEN = strtoupper(substr($model->NameEN,0,1));
		
		if(!$model->Description){
			throw new BusinessException('品牌描述不能为空');
		}
		if(!$model->BrandType){
			throw new BusinessException('品牌类别不能为空',self::DATA_EMPTY);
		}
		$brandTypeArr = array();
		foreach($model->BrandType as $type){
			array_push($brandTypeArr, "[$type]");
		}
		$model->Btypes = implode(',', $brandTypeArr);
		$model->CreateBy = self::getCurrentUser()->Id;
		$model->Status = \haibao\cms\model\data\Brand::STATUS_ENABLE;
		$model->CreateTime = new \DateTime();
		
		$model->TagId = self::insertBrandTag($model);
		$data = new \haibao\cms\data\Brand();
		$imageData = new \haibao\cms\data\ImageLibrary();
		$imageData->updateImageStatus($model->ImgId,\haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,self::getCurrentUser()->UserName);
		$data->add($model);
		
		self::checkOperation(self::FUNC_CODE_BRAND_ADD);
	}
	/**
	 * 插入品牌标签
	 */
	public static function insertBrandTag($model){
		$tagModel = new \haibao\cms\model\data\Tag();
		$tagData = new \haibao\cms\data\Tag();
		
	    $tagModel->TagName  = $model->NameCN;
	    $tagModel->Alias = $model->NameCN;
	    $tagModel->ParentId = TagModel::BRAND_TAG_ID;
	    $tagModel->TagLevel = TagModel::DEFAULT_TAG_LEVEL;
	    $tagModel->QuanPin = $model->NameCN;
	    $tagModel->JianPin = $model->NameEN;
	    $tagModel->TagType = TagModel::TYPE_BRAND;
	    $tagModel->TagStatus = TagModel::STATUS_ENABLE;
	    $tagModel->Creator =  self::getCurrentUser()->UserName;
	    $tagModel->CreateTime = time();
	    $tagModel->LastModified = $tagModel->Creator;
	    $tagModel->TopId = TagModel::BRAND_TAG_ID;
	    $tagModel->TopicId = $tagData->getMaxTopicId() + 1;
	    $tagModel->Suser =  $tagModel->Creator;
	    
	    TagBusiness::setTagUrl($tagModel);
	    $tagData->add($tagModel);
	    $tagData->clearCache();
	   	return $tagModel->Id;	
	}
	
	/**
	 * @param  int/array $brandId
	 * @return model/array(model)
	 */
	public static function getBrandById($brandId,$isLock = false){
		$data = new \haibao\cms\data\Brand();
		return $data->getBrandById($brandId,$isLock);
	}
	
	/**
	 * 保存品牌
	 * @param \haibao\cms\model\data\Brand $model
	 */
	public static function saveBrand($model,$newImgId,$oldImgId){
		self::checkOperation(self::FUNC_CODE_BRAND_SAVE);
		
		if(!$model || !($model instanceof  \haibao\cms\model\data\Brand)){
			throw new BusinessException('品牌不能为空',self::DATA_EMPTY);
		}
		if(!$model->Id){
			throw new BusinessException('品牌Id不能空',self::DATA_EMPTY_ID);
		}
		if(!$newImgId){
			throw new BusinessException('图片不能为空',self::DATA_EMPTY_ID);
		}
		/* if(!$oldImgId){
			throw new BusinessException('图片不能为空',self::DATA_EMPTY_ID);
		} */
		
		if(!$model->NameCN){
			throw  new BusinessException('品牌中文名不能为空',self::DATA_EMPTY_NAME);
		}else{
			$chineseSpell = new ChineseSpell();
			$chSpellFull  = $chineseSpell->getFullSpell($model->NameCN," ");
			$model->PinyinCN = strtolower(substr($chSpellFull,0,1));
		}
		if(!self::checkTagId($model->TagId,$model->Id)){
			throw new BusinessException('标签不存在',self::DATA_ERROR);
		}
		if(self::hasSameBrandName($model->NameCN, null,$model->Id)){
			throw new BusinessException('品牌中文名已存在',self::DATA_ERROR_NAME_EXIST);
		}
		if(!$model->NameEN){
			$model->NameEN = $chSpellFull;
		}
		$model->PinyinEN = strtoupper(substr($model->NameEN,0,1));
		
		if(!$model->Description){
			throw new BusinessException('品牌描述不能为空');
		}
		if(!$model->BrandType){
			throw new BusinessException('品牌类别不能为空',self::DATA_EMPTY);
		}
		$brandTypeArr = array();
		foreach($model->BrandType as $type){
			array_push($brandTypeArr, "[$type]");
		}
		$model->Btypes = implode(',', $brandTypeArr);
		$model->UpdateTime = new \DateTime();
		$model->LastEditeBy = self::getCurrentUser()->Id;
		
		$data = new \haibao\cms\data\Brand();
		if($newImgId !== $oldImgId){
 			$imageData = new \haibao\cms\data\ImageLibrary();
 			if($oldImgId){
 				$imageData->updateImageStatus($oldImgId,\haibao\cms\model\data\ImageLibrary::STATUS_DISABLE,self::getCurrentUser()->UserName);
 			}
 			$imageData->updateImageStatus($newImgId,\haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,self::getCurrentUser()->UserName);
		}
		
		$model->ImgId = $newImgId;
		
		$data->save($model);
		
		self::saveOperateHistory(self::FUNC_CODE_BRAND_SAVE, $model->toArray());
	}
	
	public static function checkTagId($tagId,$brandId){
		$brandInfo = self::getBrandById($brandId);
		if($brandInfo->TagId == $tagId){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * @param \haibao\cms\model\filter\BrandFilter
	 */
	public static function getBrandByFilter($filter){
	   $data = new \haibao\cms\data\Brand();
	   return $data->getAll($filter);
	}
	
	/**
	 * 删除品牌
	 */
	public static function deleteBrandById($brandId,$status = \haibao\cms\model\data\Brand::STATUS_DISABLE){
		self::checkOperation(self::FUNC_CODE_BRAND_DELETE);
		if(!$brandId){
			throw new BusinessException('品牌Id不能为空',self::DATA_EMPTY_ID);
		}
		$brandData = new \haibao\cms\data\Brand();
		$isLock = $status?true:false;
		$brandInfo = $brandData->getBrandById($brandId,$isLock);
		if($brandInfo->ImgId){
			$imageData = new \haibao\cms\data\ImageLibrary();
			$imageData->updateImageStatus($brandInfo->ImgId,$status,self::getCurrentUser()->UserName);
		}
		$brandData->deleteBrandById($brandId,$status);
		
		self::saveOperateHistory(self::FUNC_CODE_BRAND_DELETE, array('Id' =>$brandId));
	}
	
	/**
	 * 其他模块调用添加品牌
	 * @param : \haibao\cms\model\data\Brand
	 */
	public static function AddBrandForOther($brandModel,$syncOld = true){
		
		if(!$brandModel || !($brandModel instanceof  \haibao\cms\model\data\Brand)){
			throw new BusinessException('品牌不能为空',self::DATA_EMPTY);
		}
		if(!$brandModel->NameCN){
			throw  new BusinessException('品牌中文名不能为空',self::DATA_EMPTY_NAME);
		}else{
			$chineseSpell = new ChineseSpell();
			$chSpellFull  = $chineseSpell->getFullSpell($brandModel->NameCN," ");
			$brandModel->PinyinCN = strtolower(substr($chSpellFull,0,1));
		}
		
		if(self::hasSameBrandName($brandModel->NameCN, null)){
			throw new BusinessException('品牌中文名已存在',self::DATA_ERROR_NAME_EXIST);
		}
		if(!$brandModel->NameEN){
			$brandModel->NameEN = $chSpellFull;
		}
		$brandModel->PinyinEN = strtoupper(substr($brandModel->NameEN,0,1));

		if(!$brandModel->Description){
			$brandModel->Description = '';
		}
		
		if(!$brandModel->BrandType){
			$brandModel->BrandType = array(BrandModel::BRAND_TYPE_WOMENSWEAR);

		}
		$brandTypeArr = array();
		foreach($brandModel->BrandType as $type){
			array_push($brandTypeArr, "[$type]");
		}
		$brandModel->Btypes = implode(',', $brandTypeArr);
		
		$brandModel->Status = \haibao\cms\model\data\Brand::STATUS_ENABLE;
		$brandModel->CreateTime = new \DateTime();
		
		$data = new \haibao\cms\data\Brand();
		return $data->addBrandForOther($brandModel,$syncOld);
	}
	/**
	 * 创建设计师的空间
	 */
	public static function createDesignerSpace($brandModel){
		$data = new \haibao\cms\data\Brand();
		$data->createDesignerSpace($brandModel);
	}
	/*
	 * 根据标签ID更新品牌信息
	 * @param : object \haibao\cms\model\data\Brand $brandModel
	 */
	public static function UpdateBrandByTagId($brandModel){
		
		$data = new \haibao\cms\data\Brand();
		$arrSet = array(
				BrandFilter::CONDITION_FIELD_NAME_NAMECN =>$brandModel->NameCN,
				BrandFilter::CONDITION_FIELD_NAME_NAMEEN =>$brandModel->NameEN
		);
		$filter = new \haibao\cms\model\filter\Brand();
		$filter->where(BrandFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_EQUAL,$brandModel->TagId);
		$brand = $data->getOne($filter);
		if($brand && self::hasSameBrandName($brandModel->NameCN, null,$brand->Id)){
			throw new BusinessException('品牌中文名已存在',self::DATA_ERROR_NAME_EXIST);
		}
		
		$data->updateBrandByFilter($arrSet,$filter,$brandModel);
	}
	/**
	 * @param int $tagId
	 */
	public static function deleteBrandByTagId($tagId){
		if($tagId){
			$data = new \haibao\cms\data\Brand();
			$brandFilter = new BrandFilter();
			$brandFilter->where(BrandFilter::CONDITION_FIELD_NAME_TAG_ID, Condition::CONDITION_EQUAL, $tagId);
			$data->deleteBrandByTagId($tagId,$brandFilter);
		}
	}
	/**
	 * 删除设计师空间
	 */
	public static function deletecDesignerSpaceByTagId($tagId){
		if($tagId){
			$data = new \haibao\cms\data\Brand();
			$brandFilter = new BrandFilter();
			$brandFilter->where(BrandFilter::CONDITION_FIELD_NAME_TAG_ID, Condition::CONDITION_EQUAL, $tagId);
			$data->deletecDesignerSpaceByTagId($tagId,$brandFilter);
		}
	}
}
